package msu.erdc.hci;

import android.app.AlertDialog;
import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class Media extends Fragment{
	
	TextView breadcrumb;
	String breadcrumbText;
	final String BREADCRUMB_SUFFIX = "/Media";
	GridView grid;
	ImageAdapter imageAdapter;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		View view =  inflater.inflate(R.layout.media, container, false);
		
		breadcrumb = (TextView) view.findViewById(R.id.mediaBreadcrumb);
		breadcrumb.setText(breadcrumbText + BREADCRUMB_SUFFIX);
		
		grid = (GridView) view.findViewById(R.id.mediaGrid);
		grid.setAdapter(imageAdapter);
		
        return view;
	}
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        if(arguments!=null && arguments.containsKey("breadcrumbText"))
        	breadcrumbText = arguments.getString("breadcrumbText");
        else
        	breadcrumbText = "";
        imageAdapter = new ImageAdapter(getActivity(), 1);
	}
	
	public void newButtonClicked()
	{
		new AlertDialog.Builder(getActivity())
		.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, new String[]{"Photo","Video","Sound"}), new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				addNewImage();
			}
		})
		.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				//do nothing
			}
		})
		.show();
	}
	
	private void addNewImage()
	{
		imageAdapter.newImage();
		imageAdapter.notifyDataSetChanged();
	}
	
	public class ImageAdapter extends BaseAdapter 
	{
	    private Context context;
	    private int count;

	    public ImageAdapter(Context c, int initialCount) 
	    {
	        context = c;
	        count = initialCount+1;
	    }

	    //---returns the number of images---
	    public int getCount() {
	        return count;
	    }
	    
	    public void newImage()
	    {
	    	count++;
	    }

	    //---returns the ID of an item--- 
	    public Object getItem(int position) {
	        return imageIDs[getCorrectedPosition(position)];
	    }

	    public long getItemId(int position) {
	        return position;
	    }
	    
	    private int getCorrectedPosition(int position)
	    {
	    	return (position-1)%imageIDs.length;
	    }

	    //---returns an ImageView view---
	    public View getView(int position, View convertView, ViewGroup parent) 
	    {
	    	if(position>0)
	    	{
		        ImageView imageView;
		        if (convertView == null || !(convertView instanceof ImageView)) {
		            imageView = new ImageView(context);
		            imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
		            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
		            imageView.setPadding(5, 5, 5, 5);
		        } else {
		            imageView = (ImageView) convertView;
		        }
	        	imageView.setImageResource(imageIDs[getCorrectedPosition(position)]);
		        return imageView;
	    	}
	    	else
	    	{
	    		Button newMediaButton = new Button(context);
	    		newMediaButton.setOnClickListener(new OnClickListener() {
					
					public void onClick(View v) {
						newButtonClicked();
					}
				});
	    		newMediaButton.setText("+\nAdd");
	    		newMediaButton.setGravity(Gravity.CENTER);
	    		newMediaButton.setLayoutParams(new GridView.LayoutParams(150, 150));
	    		return newMediaButton;
	    	}
	    }

	    Integer[] imageIDs = {
	            R.drawable.cars_reliant_robin,
	            R.drawable.ford_model_t_cars_parades,
	            R.drawable.san_diego_autoshow
	    };
	}   

}
